<?php
header('content-type:text/html;charset=utf-8;');
$conn = mysqli_connect('127.0.0.1','root','root','jd');
$id = $_REQUEST['id'];
$name = $_REQUEST['name'];
$img = $_REQUEST['img'];
$price = $_REQUEST['price'];
$num = $_REQUEST['num'];

$sql = "SELECT * FROM `cart` WHERE `id`='$id'";
$res = mysqli_query($conn,$sql);
$rows = mysqli_num_rows($res);
if($rows>0){
	$row = mysqli_fetch_assoc($res);
	$num = $row['num']+$num;
	$sql = "UPDATE `cart` SET `num`='$num' WHERE `id`='$id'";
}else{
	$sql = "INSERT INTO `cart` (`id`,`name`,`img`,`price`,`num`) VALUES ('$id','$name','$img','$price','$num')";
}

$result = mysqli_query($conn,$sql);
if($result){
	echo json_encode(array("code"=>1));
}else{
	echo json_encode(array("code"=>0));
}

?>